/**
 * This class will initiate new client connection.
 * Every instance of this class will be able to send and receive message to and from the server.
 * 
 * @author Gil Levinson
 * @author Boris Fradlis
 * @author Yogev Levi
 * @author Hila Porat
 */


package system;
import java.io.Serializable;
import client.ChatClient;
import common.ChatIF;

//Start of VcpClient ******************************************************************************

public class VcpClient implements ChatIF{
	
	//Class variables 
	
	final public static int DEFAULT_PORT = 5555;
	ChatClient myClient;
	
	
	/**
	 * Will construct instance of the chat client 
	 * @param host - The server to connect to
	 * @param port - The port number to connect on.
	 * @throws Exception - Constructor of ChatClient can throw IOException
	 */
		
	public VcpClient(String host, int port) throws Exception{
		myClient = new ChatClient(host,port,this);
	}
	
	// class methods *******************************************************************************************
	
	/**
	 * Used to display objects onto a UI.
	 */
	
	 	@Override
	public void display(String message) {
		System.out.println(message);		
	}
	
	 /**
	  * Terminate the connection with the server.
	  * @throws Exception - IOException - if an I/O error occurs when closing.
	  */
		 	
	public void terminateConnection() throws Exception
	{
	  myClient.closeConnection();
	}
	
	/**
	 * Opens the connection with the server. If the connection is already opened, this call has no effect.
	 * @throws Exception - IOException - if an I/O error occurs when opening.
	 */
	  
	public void renewConnection() throws Exception
	{
		myClient.openConnection();
	}
	
	/**
	 * This method handles all data coming from the UI 
	 * @param msg - message from client UI
	 */
	
	public synchronized void send(Serializable msg)
	{
		myClient.handleMessageFromClientUI(msg);
		
	}
	
	/**
	 * Get the object sent from the server
	 * @return the object sent from the server
	 */
	  
	public synchronized Object getResult()
	{
		return (myClient.getResult());   
	}

	/**
	 * @return the myClient
	 */
	public ChatClient getMyClient() {
		return myClient;
	}

	/**
	 * @param myClient the myClient to set
	 */
	public void setMyClient(ChatClient myClient) {
		this.myClient = myClient;
	}

}//End of VcpClient
